<%--
  Created by IntelliJ IDEA.
  User: hp
  Date: 2018/12/10
  Time: 15:39
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <jsp:include page="${pageContext.request.contextPath}/common/tag.jsp"/>
    <script>
        $(function () {
            /*绑定类型下拉框*/
            $.post("${pageContext.request.contextPath}/brand/query",function (data) {
                $.each(data,function (index,item) {
                    $("#brandid").append("<option value=\""+item.bid+"\">"+item.bname+"</option>");
                    //$("#fenlei").append("<option value=\""+item.fid+"\">"+item.fname+"</option>");
                })
            });
            /*绑定单价选择单位的下拉框*/
            /*$.post("{pageContext.request.contextPath}/company/querydanwei",function (data) {
               $.each(data,function (index,item) {
                   $("#danwei").append("<option value=\""+item.cid+"\">"+item.cname+"</option>");
               })
            });*/
            /*table表格*/
            $("#shoptable").bootstrapTable({
                url : '${pageContext.request.contextPath}/shopinfo/query', //路径
                toolbar : '#add,#pldel',
                striped : true,
                pageSize : 5, //设置每页显示的行数
                pageList:[2,4,8,16], //设置可供选择的页面数据条数
                pageNumber:1, //如果设置了分页，首页页码。
                height : 610,  //表格高度
                pagination : true, //开启分页  默认情况下，是客户端分页的
                sidePagination:'server',  //启用服务器端分页
                queryParams:function(params){
                    var temp = {  //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
                        limit: params.limit,  //页面大小
                        offset: params.offset, //页码
                        sName:$("#shopname").val(),
                        brandid:$("#brandid").val()
                    };
                    return temp;
                },
                columns : [
                    {checkbox : true},
                    {field : 'snumber',title : '商品编号',align : 'center'},
                    {field : 'sName',title : '商品名称',align : 'center'},
                    {field : 'fenlei.fname',title : '商品分类',align : 'center'},
                    {field : 'brand.bname',title : '品牌',align : 'center'},
                    {field : 'jinjia',title : '进价',align : 'center'},
                    {field : 'shoujia',title : '销售价',align : 'center'},
                    {field : 'stockNum',title : '总库存',align : 'center'},
                    //{field : 'cname',title : '商品单位',align : 'center'},
                    {title : '操作',align : 'center',
                        formatter : function (value,row,index) {
                            return "<button class=\"btn btn-primary\" data-toggle=\"modal\" href=\"#tejia\" onclick='tejia("+index+")'>特价</button>"

                        }
                    }
                ]
            }) ;
            <!--校验框架-->
           /* $('#shopform').bootstrapValidator({
                message: 'This value is not valid',
            }) ;*/
            <!--校验框架-->
            $('#shopform').bootstrapValidator({
                message: 'This value is not valid',
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                fields: {
                    sName: {
                        message: '请输入商品名称',
                        validators: {
                            notEmpty: {
                                message: '商品名称不能为空！'
                            }
                        }
                    },
                    speci: {
                        message: '请输入商品名称',
                        validators: {
                            notEmpty: {
                                message: '请输入商品规格！'
                            }
                        }
                    },
                    typeid: {
                        message: '类型校验失败',
                        validators: {
                            notEmpty: {
                                message: '商品分类不能为空！'
                            },
                            callback: {
                                message: '必须选择一个类型',
                                callback: function(value, validator) {
                                    if (value == 0) {
                                        return false;
                                    } else {
                                        return true;
                                    }

                                }
                            }
                        }
                    },
                    goldMoney: {
                        validators: {
                            notEmpty: {
                                message: '请输入商品进价金额！'
                            },
                            numeric: {message: '进价金额只能输入数字'}
                        }
                    },
                    saleMoney: {
                        validators: {
                            notEmpty: {
                                message: '请输入商品售价金额！'
                            },
                            numeric: {message: '售价金额只能输入数字'}
                        }
                    },
                    stockNum: {
                        validators: {
                            notEmpty: {
                                message: '请输入商品库存数量！'
                            },
                            callback: {
                                message: '库存数量不能为0',
                                callback: function(value, validator) {
                                    if (value == 0) {
                                        return false;
                                    } else {
                                        return true;
                                    }
                                }
                            },
                            numeric: {message: '库存数量只能输入数字'}
                        }
                    },
                    company: {
                        message: '单位校验失败',
                        validators: {
                            notEmpty: {
                                message: '商品单位不能为空！'
                            },
                            callback: {
                                message: '必须选择一个单位',
                                callback: function(value, validator) {
                                    if (value == 0) {
                                        return false;
                                    } else {
                                        return true;
                                    }
                                }
                            }
                        }
                    },
                    dianpu: {
                        message: '单位校验失败',
                        validators: {
                            notEmpty: {
                                message: '店铺不能为空！'
                            },
                            callback: {
                                message: '必须选择一个店铺',
                                callback: function(value, validator) {
                                    if (value == 0) {
                                        return false;
                                    } else {
                                        return true;
                                    }
                                }
                            }
                        }
                    }
                }
            });
            /*特价form*/
            $('#addtejia').bootstrapValidator({
                message: 'This value is not valid',
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                fields: {
                    spMoeny: {
                        validators: {
                            notEmpty: {
                                message: '请输入特价金额！'
                            },
                            callback: {
                                message: '特价金额不能为0',
                                callback: function(value, validator) {
                                    if (value == 0) {
                                        return false;
                                    } else {
                                        return true;
                                    }
                                }
                            },
                            numeric: {message: '库存数量只能输入数字'}
                        }
                    },

                }
            });
            /*当商品名称框失去焦点的时候触发*/
            /*$("#sName").blur(function () {
                var name = $(this).val();
                $.post("{pageContext.request.contextPath}/shop/qinjin",{"name":name},function (data) {
                    if(data.i==1){
                        $("#baocun").attr('disabled','disabled');
                        swal("提示信息", "该商品已存在您可以!", "error");
                    }
                    //console.log(data);
                })
            });*/
        });

        /*点击搜索*/
        function serch() {
            //返回表格的 Options。
            var opts = $("#shoptable").bootstrapTable("getOptions") ;
            var limit = opts.pageSize ;
            var offset = (opts.pageNumber-1) * limit;
            var shopname = $("#shopname").val() ;
            var typeid = $("#type").val();
            $.post("${pageContext.request.contextPath}/shop/query",{'name':shopname,'typeid':typeid,'limit':limit,'offset':offset},function (data) {
                // var mydata = eval("("+data+")") ;
                //重新绑定表格数据
                $("#shoptable").bootstrapTable('load',data) ;
            })
        }
    </script>
</head>
<body style="margin:20px 10px;overflow-y: hidden;">
    <div class="" style="margin: 0px 10px">
        <%--<button id="add" onclick="addinfo()" class="btn btn-primary" data-toggle="modal" href="#shopmodal">添加</button>&nbsp;&nbsp;
        <button id="pldel" onclick="pldel()" class="btn btn-primary" style="margin-left: 20px;">批量删除</button>--%>
        <form class="form-inline" style="margin-top: 20px;text-align: right">
            <div class="form-group">
                <label style="font-size: 15px;">品牌：</label>&nbsp;
                <select name="name" id="brandid" class="form-control">
                    <option value="0">请选择</option>
                </select>
            </div> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <div class="form-group">
                <label for="shopname">商品名称：</label>&nbsp;
                <input type="text" class="form-control" id="shopname" placeholder="双汇火腿" style="height: 40px;">
            </div>
            <button onclick="serch();" class="btn btn-primary btn-lg" type="button"><span class="glyphicon glyphicon-search">搜索</span></button>
        </form>
        <table id="shoptable"></table>
        <%--添加模态框--%>
       <%-- <div class="modal fade" id="shopmodal">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title">添加商品信息</h4>
                    </div>
                    <div class="modal-body">
                        <form id="shopform" class="form-horizontal"  method="post" role="form">
                            &lt;%&ndash;商品id&ndash;%&gt;
                            <input type="hidden" value=""  id="id">
                            &lt;%&ndash;得到当前页码&ndash;%&gt;
                                <input type="hidden" name="pn">
                            <div class="form-group">
                                <label for="sName">商品名称：</label>
                                <input type="text" class="form-control" name="sName" id="sName" placeholder="请输入商品名称">
                            </div>
                            <div class="form-group">
                                <label for="guige">规格：</label>
                                <input type="text" class="form-control" name="speci" id="guige" placeholder="200ml">
                            </div>
                            <div class="form-group">
                                <label for="fenlei">商品分类：</label>
                                <select class="form-control" name="typeid" id="fenlei">
                                    <option value="0">请选择</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <label for="jinjia">商品进价：</label>
                                <input type="text" class="form-control" name="goldMoney" id="jinjia" placeholder="2.5">
                            </div>
                            <div class="form-group">
                                <label for="shoujia">商品售价：</label>
                                <input type="text" class="form-control" name="saleMoney" id="shoujia" placeholder="5">
                            </div>
                            <div class="form-group">
                                <label for="kucun">库存数量：</label>
                                <input type="text" class="form-control" name="stockNum" id="kucun" placeholder="10">
                            </div>
                            <div class="form-group">
                                <label for="danwei">单位：</label>
                                <select class="form-control" name="company" id="danwei">
                                    <option value="0">请选择</option>
                                </select>
                            </div>
                                <div class="form-group">
                                    <label for="dianpu">店铺：</label>
                                    <select class="form-control" name="dianpu" id="dianpu">
                                        <option value="0">请选择</option>
                                        <option value="1">总店</option>
                                        <option value="2">分店1</option>
                                    </select>
                                </div>
                            <input type="hidden" value="1" id="jiagai">
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                        <button type="button" class="btn btn-primary" id="baocun" onclick="baocun()">保存</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div>--%><!-- /.modal -->
        <%--特价--%>
           <%-- <a class="btn btn-primary" data-toggle="modal" href="#tejia">Trigger modal</a>--%>
            <div class="modal fade" id="tejia">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                            <h4 class="modal-title">添加特价</h4>
                        </div>
                        <div class="modal-body">
                            <form id="addtejia" class="form-horizontal"  method="post" role="form">
                                <h4 style="text-align: center;color: red">如果特价中有该商品则直接修改特价金额</h4>
                                <div class="form-group">
                                    <label>特价金额：</label>
                                    <input type="text" class="form-control" name="spMoeny" placeholder="请输入特价金额">
                                    <input type="hidden" class="form-control" name="sid">
                                    <input type="hidden" class="form-control" name="sName">
                                    <input type="hidden" class="form-control" name="sTypeid">
                                    <input type="hidden" class="form-control" name="pinpai">
                                </div>
                            </form>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                            <button type="button" class="btn btn-primary" onclick="tjbaocun()">保存</button>
                        </div>
                    </div><!-- /.modal-content -->
                </div><!-- /.modal-dialog -->
            </div><!-- /.modal -->
    </div>
</body>
<script>
    function baocun(){
        var jgid = $("#jiagai").val();
        if(jgid==1){
            var option = $("#shoptable").bootstrapTable("getOptions");
            var pn = option.pageNumber;
            //alert(pn);
            $("input[name='pn']").val(pn);

            //启用较验
            $("#shopform").data("bootstrapValidator").validate();
            //验证是否通过true/false
            var flag = $("#shopform").data("bootstrapValidator").isValid() ;
            if (flag){
                //alert("添加");
                //序列化表单
                var formStr=$("#shopform").serialize();
                //处理表单控件的中文乱码
                params = decodeURIComponent(formStr,true);
                //console.log(params);
                $("#shopmodal").modal('hide');
                var dianpu = $("#dianpu").val();
                alert(params);
                $.ajax({
                    type:'post',
                    url: '${pageContext.request.contextPath}/shop/shopInsert',
                    data : params,
                    success:function (data) {
                        document.getElementById("shopform").reset();
                        $("#shopform").data('bootstrapValidator').resetForm();//清除当前验证的关键之处
                        /*隐藏对话框*/
                        $("#shopmodal").modal('hide');
                        /*从新绑定表格*/
                        $("#shoptable").bootstrapTable('load',data);
                        //提示增加成功
                        swal("提示信息", "增加成功!", "success");
                    }
                });
            }
        }else{
            var option = $("#shoptable").bootstrapTable("getOptions");
            var pn = option.pageNumber;
            //alert(pn);
            $("input[name='pn']").val(pn);
            alert("修改");
            //启用较验
            $("#shopform").data("bootstrapValidator").validate();
            //验证是否通过true/false
            var flag = $("#shopform").data("bootstrapValidator").isValid() ;
            var id = $("#id").val();
            if (flag){
                var id = $("#id").val();
                var name = $("#sName").val();
                var guige = $("#guige").val();
                var fenlei = $("#fenlei").val();
                var jinjia = $("#jinjia").val();
                var shoujia = $("#shoujia").val();
                var kucun = $("#kucun").val();
                var danwei = $("#danwei").val();
                var pn = $("[name='pn']").val();
                alert(jinjia);
                //序列化表单
                //var formStr=$("#shopform").serialize();
                //处理表单控件的中文乱码
                //params = decodeURIComponent(formStr,true);
                //console.log(params);
                $("#shopmodal").modal('hide');
                $.ajax({
                    type:'post',
                    url: '${pageContext.request.contextPath}/shop/shopUpdate',
                    data : {"id":id,"name":name,"guige":guige,"typeid":fenlei,"jinjia":jinjia,"shoujia":shoujia,"kucun":kucun,"danwei":danwei,'pn':pn},
                    success:function (data) {
                        document.getElementById("shopform").reset();
                        $("#shopform").data('bootstrapValidator').resetForm();//清除当前验证的关键之处
                        /*隐藏对话框*/
                        $("#shopmodal").modal('hide');
                        /*从新绑定表格*/
                        $("#shoptable").bootstrapTable('load',data);
                        //提示增加成功
                        swal("提示信息", "修改成功!", "success");
                    }
                });
            }
        }
    }
    /*点击添加按钮销毁表单*/
    function addinfo() {
        $("#jiagai").val(1);
        document.getElementById("shopform").reset();
        $("#shopform").data('bootstrapValidator').resetForm();//清除当前验证的关键之处
    }
    /*获取修改的值*/
    function huoqu(id) {
        document.getElementById("shopform").reset();
        $("#shopform").data('bootstrapValidator').resetForm();//清除当前验证的关键之处
        $("#jiagai").val(2);
        $.post("${pageContext.request.contextPath}/shop/huoquShopInfo",{'id':id},function (data) {
            /*console.log(data);*/
            var shuzu = [];
            $.each(data,function (i) {
                shuzu = data[i];
            });
            $("#id").val(shuzu.id);
            $("#sName").val(shuzu.sname);
            $("#guige").val(shuzu.speci);
            $("#fenlei").val(shuzu.typeid);
            $("#jinjia").val(shuzu.goldMoney);
            $("#shoujia").val(shuzu.saleMoney);
            $("#kucun").val(shuzu.stockNum);
            $("#danwei").val(shuzu.company);
        });
    }

    /*普通删除*/
    function del(id,kucun) {
        if(kucun != 0){
            swal("提示信息", "不能删除，该商品还有库存!", "error");
        }else{
            var option = $("#shoptable").bootstrapTable("getOptions");
             var pn = option.pageNumber;
             $("input[name='pn']").val(pn);
             swal({
                 title: "您确定要删除这条信息吗",
                 text: "删除后将无法恢复，请谨慎操作！",
                 type: "warning",
                 showCancelButton: true,
                 confirmButtonColor: "#DD6B55",
                 confirmButtonText: "删除",
                 closeOnConfirm: false
             }, function () {
                 $.post("${pageContext.request.contextPath}/shop/delById",{"id":id,"pn":pn},function (data) {
                    /!*从新绑定表格*!/
                    $("#shoptable").bootstrapTable('load',data);
                    //提示增加成功
                    swal("提示信息", "删除成功!", "success");
                });
            });
        }

    }
    /*批量删除*/
    function pldel() {
        var option = $("#shoptable").bootstrapTable("getOptions");
        var pn = option.pageNumber;
        var rows = $("#shoptable").bootstrapTable("getSelections");
        /*console.log(rows);*/
        var ids = "";
        if(rows.length==0){
            //提示增加成功
            swal("提示信息", "请选择一条数据要删除的记录!", "error");
            //alert("请选择一条数据要删除的记录");
            return;
        }else{
            swal({
                title: "您确定要删除这条信息吗",
                text: "删除后将无法恢复，请谨慎操作！",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "删除",
                closeOnConfirm: false
            },function () {
                $.each(rows,function () {
                    //alert(this.stockNum);
                    if(this.stockNum!=0){
                        swal("提示信息", "请选择库存为0的商品删除!", "error");
                    }else{
                        ids+=this.id+",";
                        $.ajax({
                            type:'post',
                            url: '${pageContext.request.contextPath}/shop/plDel',
                            data : {"ids":ids,"pn":pn},
                            success:function (data) {
                                //从新绑定表格
                                $("#shoptable").bootstrapTable('load',data);
                                //提示增加成功
                                swal("提示信息", "批量删除成功!", "success");
                            }
                        });
                    }
                });
            });
        }
    }

    function tejia(index) {
        $("#shoptable").bootstrapTable("uncheckAll");
        $("#shoptable").bootstrapTable("check",index);
        var rows = $("#shoptable").bootstrapTable('getSelections')[0];
        $("[name='sid']").val(rows.snumber);
        $("[name='sName']").val(rows.sName);
        $("[name='sTypeid']").val(rows.stypeid);
        $("[name='pinpai']").val(rows.sbrand);
    }
    function tjbaocun() {
        //启用较验
        $("#addtejia").data("bootstrapValidator").validate();
        //验证是否通过true/false
        var flag = $("#addtejia").data("bootstrapValidator").isValid() ;
        if(flag){
            //序列化表单
            var formStr=$("#addtejia").serialize();
            //处理表单控件的中文乱码
            params = decodeURIComponent(formStr,true);
            $.ajax({
                type:'post',
                url: '${pageContext.request.contextPath}/special/insert',
                data : params,
                success:function (data) {
                    document.getElementById("addtejia").reset();
                    $("#addtejia").data('bootstrapValidator').resetForm();//清除当前验证的关键之处
                    /*隐藏对话框*/
                    $("#tejia").modal('hide');
                    //提示增加成功
                    swal("提示信息", "增加成功!", "success");
                }
            });
        }
    }
</script>
</html>
